﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;

using Husb.Data;
using Husb.DataUtil;

using GroundWater.Data;
using AbnormalTable = GroundWater.Data.AbnormalDataSet.WaterlevelAbnormalDataTable;
using AbnormalRow = GroundWater.Data.AbnormalDataSet.WaterlevelAbnormalRow;

namespace GroundWater.DataAccess
{
    public class WaterlevelAbnormalAdapter : DataAccessAdapter<AbnormalDataSet, AbnormalTable, AbnormalRow>
    {
        public WaterlevelAbnormalAdapter()
        {
            this.TableName = "WaterlevelAbnormal";

            GetAllStoredProc = SqlStatements.Abnormal_FetchAll;
            InsertStoredProc = SqlStatements.Abnormal_Insert;
            UpdateStoredProc = SqlStatements.Abnormal_Update;
            DeleteStoredProc = SqlStatements.Abnormal_Delete;

            this.GetByIdStoredProc = SqlStatements.Abnormal_FetchById;
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "PreviousWaterlevel", DbType.Decimal, "PreviousWaterlevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "CurrentWaterlevel", DbType.Decimal, "CurrentWaterlevel", DataRowVersion.Current);
            db.AddInParameter(cmd, "Difference", DbType.Decimal, "Difference", DataRowVersion.Current);
            db.AddInParameter(cmd, "WarningDifference", DbType.Decimal, "WarningDifference", DataRowVersion.Current);

            db.AddInParameter(cmd, "MonitoringDate", DbType.DateTime, "MonitoringDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "WellCode", DbType.String, "WellCode", DataRowVersion.Current);
            db.AddInParameter(cmd, "MonitoringId", DbType.Int32, "MonitoringId", DataRowVersion.Current);
            db.AddInParameter(cmd, "ExamineDate", DbType.DateTime, "ExamineDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Examiner", DbType.String, "Examiner", DataRowVersion.Current);

            
        }

        protected override void PopulateUpdateParameters(Database db, DbCommand cmd)
        {
            this.PopulateInsertParameters(db, cmd);
            db.AddInParameter(cmd, "Id", DbType.Int32, "Id", DataRowVersion.Current);
        }

        public AbnormalRow FetchByMonitoringId(string wellCode, DateTime monitoringTime, decimal warningDifference)
        {
            List<DatabaseParameter> ps = new List<DatabaseParameter>();
            ps.Add(new DatabaseParameter("WellCode", DbType.String, wellCode));
            ps.Add(new DatabaseParameter("MonitoringDate", DbType.DateTime, monitoringTime));
            ps.Add(new DatabaseParameter("WarningDifference", DbType.Decimal, warningDifference));

            DataTable table = GetTable(SqlStatements.Abnormal_FetchByMonitoringId, ps, false, false, null);

            return table.Rows.Count > 0 ? (AbnormalRow)table.Rows[0] : null;

        }

        public AbnormalTable FetchByTimeSpan(string startDate, string endDate, decimal warningDifference)
        {
            endDate = DateTime.Parse(endDate).AddDays(1).ToString("yyyy-MM-dd");
            List<DatabaseParameter> ps = new List<DatabaseParameter>();
            ps.Add(new DatabaseParameter("StartMonitoringDate", DbType.String, startDate));
            ps.Add(new DatabaseParameter("EndMonitoringDate", DbType.String, endDate));
            ps.Add(new DatabaseParameter("WarningDifference", DbType.Decimal, warningDifference));

            AbnormalTable table = GetTable(SqlStatements.Abnormal_FetchByTimeSpan, ps, false, false, null);

            return table;

        }

        //public AbnormalTable FetchPaged(string wellCode, string startDate, string endDate, string sortExpression, int maximumRows, int startRowIndex)
        //{
        //    if (startRowIndex == 0) startRowIndex++;
        //    int end = startRowIndex + maximumRows - 1;
        //    endDate = DateTime.Parse(endDate).AddDays(1).ToString("yyyy-MM-dd");
        //    string sql = String.Format(SqlStatements.Monitoring_FetchPaged, wellCode, startDate, endDate, sortExpression, startRowIndex.ToString(), end.ToString());
        //    AbnormalTable table = this.GetTable(sql, new List<DatabaseParameter>(), false, false, null);
        //    return table;
        //}

        //public int FetchRowCount(string wellCode, string startDate, string endDate)//, string sortExpression, int maximumRows, int startRowIndex
        //{
        //    endDate = DateTime.Parse(endDate).AddDays(1).ToString("yyyy-MM-dd");
        //    string sql = String.Format(SqlStatements.Monitoring_FetchRowCount, wellCode, startDate, endDate);
        //    object o = this.GetValue(sql, new List<DatabaseParameter>(), false, false, null);
        //    if (o == null) return 0;

        //    return Convert.ToInt32(o);
        //}

        public AbnormalTable FetchPaged(string startDate, string endDate, string sortExpression, int maximumRows, int startRowIndex)
        {
            if (startRowIndex == 0) startRowIndex++;
            int end = startRowIndex + maximumRows - 1;
            endDate = DateTime.Parse(endDate).AddDays(1).ToString("yyyy-MM-dd");
            string sql = String.Format(SqlStatements.Abnormal_FetchPaged, startDate, endDate, sortExpression, startRowIndex.ToString(), end.ToString());
            AbnormalTable table = this.GetTable(sql, new List<DatabaseParameter>(), false, false, null);
            return table;
        }

        public int FetchRowCount(string startDate, string endDate)//, string sortExpression, int maximumRows, int startRowIndex
        {
            endDate = DateTime.Parse(endDate).AddDays(1).ToString("yyyy-MM-dd");
            string sql = String.Format(SqlStatements.Abnormal_FetchRowCount, startDate, endDate);
            object o = this.GetValue(sql, new List<DatabaseParameter>(), false, false, null);
            if (o == null) return 0;

            return Convert.ToInt32(o);
        }



    }
}
